#This script estimates the variance of counterfactuals, given the estimated variance of model parameters.
rm(list=ls())
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
.libPaths(c("/fp/homes01/u01/ec-maxwellk/R/",.libPaths()))
library(Rcpp)
library(data.table)
library(RcppArmadillo)
library(RcppParallel)
library(ggplot2)
library(parallel)
library(snow)
library(doSNOW)
library(Rmpi)
library(spousalVFIParallel)
library(unixtools)
library(readr)
library(readstata13)
library(foreign)
library(xtable)
library(scales)
library(mvtnorm)
library(bit64)
library(fixest)
library(pso)



setThreadOptions(numThreads=20,stackSize=1e07)


rm(list=ls())
Sys.setenv("PKG_CXXFLAGS"="-std=c++11")
.libPaths(c("/fp/homes01/u01/ec-maxwellk/R/",.libPaths()))
set.tempdir("/fp/homes01/u01/ec-maxwellk/tmp")
Sys.setenv(TMPDIR="/fp/homes01/u01/ec-maxwellk/tmp")
setwd('/fp/homes01/u01/ec-maxwellk/DynamicDI/replication_final')

base_dir<-getwd()



setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")

source("D2_Solve_ParallelSpouceC_pso_fixmar.R")




setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")

source("D3_Solve_Varcov.R")




setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")

source("D4_Solve_Varcov_Bootstrap.R")




setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")

library(stringr)
source("D5_Welfare_wtpdecomp.R")



setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")

library(lfe)
library(MASS)
source("D6_kmean_simulations.R")




setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")
source("./eventstudy/eventcode.R")

library(textables)
source("D7_Welfare_evalfit2.R")





setwd(base_dir)
rm(list=ls())
base_dir<-getwd()
source("./auxfiles/plotrules.R")
source("./auxfiles/evalfunctions.R")
source("Welfare_ParallelSpouseC.R")
source("./eventstudy/eventcode.R")

library(stringr)
library(ggpubr)
library(textables)
library(ggpattern)
library(ggh4x)
source("D8_Plot_wtp_decompose2.R")


